package com.letv.whatslive.mongo.dao;


import com.google.common.collect.Lists;
import com.letv.whatslive.common.utils.Constants;
import com.letv.whatslive.common.utils.DateUtils;
import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.common.utils.OperateLogUtils;
import com.letv.whatslive.model.GeneralCounter;
import com.letv.whatslive.model.User;
import com.letv.whatslive.model.constant.UserConstants;
import com.letv.whatslive.model.convert.GeneralCounterConvert;
import com.letv.whatslive.model.convert.UserConvert;
import com.letv.whatslive.model.utils.ObjUtils;
import com.letv.whatslive.mongo.BaseDAO;
import com.letv.whatslive.redis.JedisDAO;
import com.mongodb.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 通用计数器collection操作DAO
 * Created by haojiayao on 16-6-23.
 */
@Repository
public class GeneralCounterDAO extends BaseDAO {

    private final static Logger logger = LoggerFactory.getLogger(GeneralCounterDAO.class);

    protected String collectionName = "generalCounter";

    private static final long DEFAULT_INITIALVALUE = 1L;

    private static final long DEFAULT_INCREASE = 1L;

    @Override
    protected void init() {
        super.init(this.collectionName);
    }

    @Override
    protected long getAutoIncrementId() {
        return idGenerate.generateId(this.collectionName);
    }

    /**
     * 获取计数值
     *
     * @param counter 计数对象
     * @return
     */
    public Long getCount(GeneralCounter counter) {

        Long count = null;
        try {

            if (!exists(counter)) {

                counter.setCount(DEFAULT_INITIALVALUE);
                DBObject obj = GeneralCounterConvert.castGeneralCounterToDBObject(counter);
                this.insert(obj);
                System.out.println("insert-----------: nickName: " + counter.getField());
            }

            BasicDBObject query = new BasicDBObject();
            query.put("type", counter.getType());
            query.put("field", counter.getField());

            DBObject dbObj = this.dbCollection.findAndModify(query, new BasicDBObject("$inc",
                    new BasicDBObject("count", DEFAULT_INCREASE)));

            count = ObjectUtils.toLong(dbObj.get("count"));
        } catch (Exception e) {

            logger.error("getCount error", e);
        }

        return count;
    }

    /**
     * 检查记录是否存在
     *
     * @param counter 计数对象
     * @return
     */
    public boolean exists(GeneralCounter counter) throws Exception {

        BasicDBObject query = new BasicDBObject();
        query.put("type", counter.getType());
        query.put("field", counter.getField());

        return this.dbCollection.findOne(query) != null;
    }

    /**
     * 插入计数器
     *
     * @param counter 计数对象
     * @return
     */
    public Long insertGeneralCounter(GeneralCounter counter) {

        Long id = null;

        try {

            DBObject obj = GeneralCounterConvert.castGeneralCounterToDBObject(counter);
            id = this.insert(obj);
        } catch (Exception e) {

            logger.error("insertGeneralCounter error", e);
        }

        return id;
    }
}

